// 商机搜索表单指令
define('mainSearchFormDirective', ['angular','moment'], function (angular,moment) {
    var m = angular.module('directives');
    m.directive('mainSearchForm', [function () {
        return {
            restrict: 'AE',
            templateUrl: 'js/templates/mainSearchForm.html',
            replace: true,
            scope:{
                searchData:"=",
                businessData:"=",
                businessType:"@",
            },
            controller:['$scope','$rootScope','$route','$filter','$window','businessServers','userServers',function($scope,$rootScope,$route,$filter,$window,businessServers,userServers){
                $scope.searchData.business_type=$scope.businessType;
                if($scope.businessType==1){
                    $scope.searchData.consultant_id=$rootScope.user.id;
                }
                $scope.data=angular.copy(angular.fromJson($rootScope.user.custom_search_list));
                if($scope.data===null){
                	$scope.data={branch_company_id:1,channel_manager_id:1,consultant_id:1,education:1,email:1,failure_reason_id:1,id:1,info_source:1,invite:1,name:1,order_id:1,phone:1,positioning:1,qq:1,school:1,sign:1,sign_out:1,visit:1};
                }
                $scope.searchFormData=angular.copy($scope.data);//模态框的数据
                $scope.searchFormShow=angular.copy($scope.data);//搜索框的数据
                $scope.searchBusiness=function(){
                    $scope.searchData = $filter('simplifyJsonFilter')($scope.searchData);
                    businessServers.getBusinessList($scope.searchData).then(function(data) {
                        $scope.businessData=data;
                        for(var i=0;i<$scope.businessData.length;i++){
                            if($scope.businessData[i].time!==null){
                                $scope.businessData[i].time=moment($scope.businessData[i].time).format("YYYY-MM-DD");
                            }
                            $scope.businessData[i].follow_up_records=angular.fromJson($scope.businessData[i].follow_up_records);
                        }
                    });
                };
                $scope.setSearchForm=function(){
//                  console.log($scope.searchFormData);
                    $scope.data={};
                    $scope.data.id=$rootScope.user.id;
                    $scope.data.custom_search_list=angular.toJson($scope.searchFormData);
                    userServers.setUser($scope.data).then(function(data){
                        if(data.data){
                            $('#searchForm').modal('toggle');
                            $rootScope.user.custom_search_list=angular.copy($scope.searchFormData);
                            $window.localStorage.crm_user=JSON.stringify($rootScope.user);
                            $route.reload();
                        }else{
                            $('#myModal').modal('toggle');
                            alert("修改失败！");
                        }
                    });
                };
                $scope.searchBusiness();
            }]
        };
    }]);
});